package com.hss.recyclerviewtest.behavior;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.BounceInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

/**
 * Created by Administrator on 2018/7/16.
 */

public class HeadImageBehavior extends CoordinatorLayout.Behavior<ImageView> {
    final  String TAG = "HeadImageBehavior";
    //头像的最终大小
    private float mCustomFinalHeight;
    private float mCustomStartHeight;

    private float mCustomFinalWidth;
    private float mCustomStartWidth;


    //最终头像的Y

    private float mStartAvatarY;

    private float mStartAvatarX;

    private float mFinalAvatarY;

    private float mFinalAvatarX =80 ;

    private int mAvatarMaxHeight;

    float dependencyMAXY=0;

    private BounceInterpolator interpolator = new BounceInterpolator();
    private LinearInterpolator linearInterpolator = new LinearInterpolator();

    public HeadImageBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, ImageView child, View dependency) {
        return dependency instanceof Toolbar;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, ImageView child, View dependency) {


        if (mStartAvatarX == 0){

            mCustomStartHeight = child.getHeight();
            mCustomStartWidth = child.getWidth();

            Log.e(TAG,"dependency.getHeight( = "+dependency.getHeight());


            mCustomFinalHeight = Math.round(dependency.getHeight()*0.7);
            mCustomFinalWidth = mCustomFinalHeight;

            mStartAvatarX =   dependency.getWidth()/2- child.getWidth()/2;

            dependencyMAXY=  dependency.getY();
            mStartAvatarY = dependencyMAXY -child.getHeight()/2;

            mFinalAvatarY = ( dependency.getHeight() -mCustomFinalHeight)/2.0f;
            Log.e(TAG,"mFinalAvatarY = "+mFinalAvatarY);
            Log.e(TAG,"dependency.getHeight() = "+dependency.getHeight());


            child.setX(mStartAvatarX);
            child.setY(mStartAvatarY);


            Log.e(TAG,"mCustomStartHeight = "+mCustomStartHeight +"   mCustomStartWidth = "+mCustomStartWidth);
            Log.e(TAG,"mCustomFinalHeight = "+mCustomFinalHeight +"   mCustomFinalWidth = "+mCustomFinalWidth);

        }else {
            float percent = 1-dependency.getY()/dependencyMAXY;

            float value =  linearInterpolator.getInterpolation(percent);
            Log.e(TAG,"value = "+value);
            int cutWidth = (int) (mCustomFinalWidth +(mCustomStartWidth -mCustomFinalWidth) * (1-value));
            int cutHeight = (int) (mCustomFinalHeight +(mCustomStartHeight -mCustomFinalHeight) *(1-value));

            Log.e(TAG,"cutWidth = "+cutWidth +"   cutHeight = "+cutHeight);

//            child.setScaleX(cutWidth/mCustomStartWidth);
//            child.setScaleY(cutHeight/mCustomStartHeight);

//            child.setScaleX(cutWidth/mCustomStartWidth);
//            child.setScaleY(cutHeight/mCustomStartHeight);

            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
            layoutParams.width =cutWidth;
            layoutParams.height = cutHeight;
            child.setLayoutParams(layoutParams);


            float curY = mFinalAvatarY +(mStartAvatarY -mFinalAvatarY)*(1-value);
            Log.e(TAG,"curY = "+curY);

            child.setY(curY);
            child.setX(mFinalAvatarX+(mStartAvatarX -mFinalAvatarX)*(1-value));
        }


        return true;
    }
}
